
package at.fhj.ase.clientarch.common.util;

/**
 * @author cja, sfe | www.fh-joanneum.at | Client Architectures and Design
 */
public final class StringUtil {
  public static final int DEFAULT_STRING_LENGTH_MIN = 1;
  public static final int DEFAULT_STRING_LENGTH_MAX = 50;

  private StringUtil() {
	  throw new AssertionError("StringUtil: Util classes are not intended/allowed to be instantiated!");
  }
  
  /**
   * @param stringToBeChecked the {@link String} instance we want to check
   * @return the provided {@link String} checked for <code>null</code> and a valid length of > 0.
   * @throws IllegalArgumentException if the provided {@link String} is <code>null</code> or has a length smaller 1
   */
  public static String checkEmptyOrNull(final String stringToBeChecked) {
    if (stringToBeChecked == null) {
      throw new IllegalArgumentException("String must not be null");
    }
    return checkLengthDefaults(stringToBeChecked);
  }

  /**
   * @return <code>stringToBeChecked</code> if it has a valid length, <strong>must not be <code>null</code></strong>
   */
  public static String checkLength(final String stringToBeChecked, final int minLength, final int maxLength) {
    if (stringToBeChecked.length() < minLength || stringToBeChecked.length() > maxLength) {
      throw new IllegalArgumentException("String must not be without length ranges (" + minLength + " - " + maxLength + " chars)");
    }
    return stringToBeChecked;
  }

  /**
   * @return <code>stringToBeChecked</code> if it has a valid length between {@value CommonUtil#DEFAULT_STRING_LENGTH_MIN} and
   * {@value CommonUtil#DEFAULT_STRING_LENGTH_MAX} <strong>must not be <code>null</code></strong>.
   */
  public static String checkLengthDefaults(final String stringToBeChecked) {
    return checkLength(stringToBeChecked, StringUtil.DEFAULT_STRING_LENGTH_MIN, StringUtil.DEFAULT_STRING_LENGTH_MAX);
  }

  /** @return <code>true</code> if the provided {@link String} is valid, otherwise <code>false</code> (using {@link StringUtil#checkEmptyOrNull(String)}) */
  public static boolean isEmptyOrNull(final String stringToBeChecked) {
    try {
      StringUtil.checkEmptyOrNull(stringToBeChecked);
      return false;
    } catch (IllegalArgumentException iae) {
      return true;
    }
  }

}
